package cn.openea.perpartner.meily.web.controller;

import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.Menu;
import cn.openea.perpartner.meily.web.entity.Role;
import cn.openea.perpartner.meily.web.entity.UserRole;
import cn.openea.perpartner.meily.web.entity.vo.MenuTree;
import cn.openea.perpartner.meily.web.service.IMenuService;
import cn.openea.perpartner.meily.web.service.IUserRoleService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2023-03-15 16:24
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {
    private static final String LOGO_IMAGE = "https://dev-1308272144.cos.ap-guangzhou.myqcloud.com/MeilyAdmin/meily_logo.png";

    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IMenuService menuService;
    @ApiOperation(value = "菜单列表")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody Menu menu)
    {

        return success(menuService.selectMenuList(menu));
    }
    @ApiOperation(value = "删除菜单")
    @GetMapping("/remove/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId)
    {
        if (menuService.selectCountMenuByParentId(menuId) > 0)
        {
            return AjaxResult.warn("存在子菜单,不允许删除");
        }
        if (menuService.selectCountRoleMenuByMenuId(menuId) > 0)
        {
            return AjaxResult.warn("菜单已分配,不允许删除");
        }
        return toAjax(menuService.removeById(menuId));
    }
    @ApiOperation(value = "新增菜单")
    @PostMapping("/add")
    public AjaxResult insertMenu(@RequestBody Menu menu)
    {
        if (menuService.checkMenuNameUnique(menu))
        {
            return error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setCreateUser(UserContext.getUserId());
        menu.setUpdateUser(UserContext.getUserId());
        return toAjax(menuService.save(menu));
    }
    @ApiOperation(value = "菜单详情")
    @PostMapping("/edit/{menuId}")
    public AjaxResult edit(@PathVariable("menuId") Long menuId)
    {
        return success( menuService.selectMenuById(menuId));
    }

    @ApiOperation(value = "修改菜单")
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody Menu menu)
    {
        menu.setUpdateUser(UserContext.getUserId());
        return toAjax(menuService.updateById(menu));
    }

    @ApiOperation(value = "角色菜单列表树")
    @PostMapping("/roleMenuTreeData")
    public AjaxResult roleMenuTreeData(@RequestBody Role role)
    {
        String userId = UserContext.getUserId();
        return success(menuService.roleMenuTreeData(role, userId));
    }

    @ApiOperation(value = "所有菜单列表树")
    @GetMapping("/menuTreeData")
    public AjaxResult menuTreeData()
    {
        String userId = UserContext.getUserId();
        return success(menuService.menuTreeData(userId));
    }
    @ApiOperation(value = "所有菜单列表树2")
    @GetMapping("/menuTreeDtoData")
    public AjaxResult menuTreeDtoData()
    {
        return success(menuService.menuTreeDtoData());
    }

    @ApiOperation(value = "选择菜单树")
    @GetMapping("/selectMenuTree/{menuId}")
    public AjaxResult selectMenuTree(@PathVariable("menuId") Long menuId)
    {
       Menu menu = menuService.selectMenuById(menuId);
        return AjaxResult.success().put("menu", menu);
    }
    @ApiOperation(value = "根据用户查询菜单")
    @GetMapping("/selectMenusByUser")
    public AjaxResult selectMenusByUser(){
        String userId = UserContext.getUserId();
        AjaxResult ajax = AjaxResult.success();
        JSONObject obj = new JSONObject();
        obj.put("title","首页");
        obj.put("href","/page/welcome.html");
        ajax.put("homeInfo",obj);
        obj = new JSONObject();
        obj.put("href","");
        obj.put("title","");
        obj.put("image",LOGO_IMAGE);
        ajax.put("logoInfo",obj);
        MenuTree menuTree = new MenuTree();
        menuTree.setTitle("常规管理");
        menuTree.setIcon("fa fa-address-book");
        menuTree.setHref("");
        menuTree.setTarget("_self");
        menuTree.setChild(menuService.selectMenusByUser(userId));
        List<MenuTree> menus = new ArrayList<>();
        menus.add(menuTree);
        ajax.put("menuInfo",menus);
        return ajax;
    }

    /*@ApiOperation(value = "根据用户获取角色菜单列表树")
    @PostMapping("/roleMenuTreeDataByUserId")
    public AjaxResult roleMenuTreeDataByUserId(@RequestBody Role role){
        // 1.根据用户id 获取 roleId
        List<UserRole> list = userRoleService.getAllRoleIdByUserId(role.getUserId());
        for(UserRole userRole : list){
            role.setRoleId(userRole.getRoleId());
        }
        return success(menuService.roleMenuTreeData(role, role.getUserId()));
    }*/

    @ApiOperation(value = "根据用户获取角色菜单列表")
    @GetMapping("/roleMenuTreeDataByUserId/{userId}")
    public AjaxResult roleMenuTreeDataByUserId(@PathVariable("userId") String userId){
        return success(menuService.roleMenuTreeData_staaff(userId));
    }

}
